import 'dart:developer';

import 'package:sports_new/entity/ai/ai_combo_entity.dart';
import 'package:sports_new/entity/ai/ai_info_entity.dart';
import 'package:sports_new/entity/ai/ai_rate_entity.dart';
import 'package:sports_new/entity/ai/chart_line_entity.dart';
import 'package:sports_new/entity/ai/chart_radar_entity.dart';
import 'package:sports_new/entity/ai/ai_lm_entity.dart';
import 'package:sports_new/entity/ai/model_entity.dart';
import 'package:sports_new/entity/match/zq_model_entity.dart';
import 'package:sports_new/entity/pay/ai_pack_entity.dart';
import 'package:sports_new/utils/utils.dart';

import '../../entity/ai/ai_date_entity.dart';
import '../../entity/ai/ai_pay_entity.dart';
import '../../entity/ai/chart_bar_entity.dart';
import '../../view/pick/ai/ai_type.dart';
import '../api_dir.dart';
import '../http/dio_utils.dart';

class AiApi {
  ///模型列表
  static Future<List<ModelEntity>?> modelAll() async {
    final result = await DioUtils.post("/${ApiDir.ai}/app/moduleList");
    if (result.statusCode == 200 && result.data['c'] == 200) {
      List<ModelEntity> data = result.data['d']
          .map<ModelEntity>((e) => ModelEntity.fromJson(e))
          .toList();
      return data;
    }
    return null;
  }

  ///2串1玩法内容
  static Future<AiComboEntity?> getCombo(String date, String type) async {
    final result = await DioUtils.post("/${ApiDir.ai}/app/comboList",
        params: {"date": date});
    if (result.statusCode == 200 && result.data['c'] == 200) {
      final data = AiComboEntity.fromJson(result.data['d']);
      return data;
    }
    return null;
  }

  ///玩法内容
  static Future<AiDateEntity?> getAi(String date, String type) async {
    final result = await DioUtils.post(
        "/${ApiDir.ai}/app/${type}List${type == AiType.lm.tag ? "WithDay" : ""}",
        params: {"date": date});
    if (result.statusCode == 200 && result.data['c'] == 200) {
      final data = AiDateEntity.fromJson(result.data['d']);
      return data;
    }
    return null;
  }

  ///柱状图详情
  static Future<ChartBarEntity> getBar(int id, String type) async {
    try {
      final result =
          await DioUtils.post("/${ApiDir.ai}/app/${type}Detail", params: id);
      if (result.statusCode == 200 && result.data['c'] == 200) {
        ChartBarEntity data = ChartBarEntity.fromJson(result.data['d']);
        if (type == AiType.assess) {
          Suggest suggest = Suggest.fromJson(result.data['d']['info']);
          data.itemInfo = [
            if (suggest.asia != null)
              ItemInfo(
                  name: "亚洲权威",
                  info: [
                    if (suggest.asia?.od?.s.hasValue == true)
                      suggest.asia?.od?.s,
                    if (suggest.asia?.od?.p.hasValue == true)
                      suggest.asia?.od?.p,
                    if (suggest.asia?.od?.f.hasValue == true)
                      suggest.asia?.od?.f
                  ].join(""),
                  rate: suggest.asia?.cnt ?? 0),
            if (suggest.europe != null)
              ItemInfo(
                  name: "欧洲权威",
                  info: [
                    if (suggest.europe?.od?.s.hasValue == true)
                      suggest.europe?.od?.s,
                    if (suggest.europe?.od?.p.hasValue == true)
                      suggest.europe?.od?.p,
                    if (suggest.europe?.od?.f.hasValue == true)
                      suggest.europe?.od?.f
                  ].join(""),
                  rate: suggest.europe?.cnt ?? 0),
            if (suggest.faith != null)
              ItemInfo(
                  name: "信心助力",
                  info: [
                    if (suggest.faith?.od?.s.hasValue == true)
                      suggest.faith?.od?.s,
                    if (suggest.faith?.od?.p.hasValue == true)
                      suggest.faith?.od?.p,
                    if (suggest.faith?.od?.f.hasValue == true)
                      suggest.faith?.od?.f
                  ].join(""),
                  rate: suggest.faith?.cnt ?? 0)
          ];
        }
        return data;
      }
    } catch (e) {
      log(e.toString());
      return ChartBarEntity();
    }
    return ChartBarEntity();
  }

  ///雷达图详情
  static Future<ChartRadarEntity> getRadar(int id, String type) async {
    final result =
        await DioUtils.post("/${ApiDir.ai}/app/${type}Detail", params: id);
    if (result.statusCode == 200 && result.data['c'] == 200) {
      final data = ChartRadarEntity.fromJson(result.data['d']);
      return data;
    }
    return ChartRadarEntity();
  }

  ///折线图详情
  static Future<ChartLineEntity> getLine(int id, String type) async {
    final result =
        await DioUtils.post("/${ApiDir.ai}/app/${type}Detail", params: id);
    try {
      if (result.statusCode == 200 && result.data['c'] == 200) {
        final data = ChartLineEntity.fromJson(result.data['d'], type: type);
        return data;
      }
    } catch (e) {
      return ChartLineEntity();
    }
    return ChartLineEntity();
  }

  ///类型购买确认
  static Future<RightCheck> getCheck(int type) async {
    final result = await DioUtils.post("/${ApiDir.ai}/app-ai-order/open",
        params: {"aiType": type});
    if (result.statusCode == 200 && result.data['c'] == 200) {
      final data = RightCheck.fromJson(result.data['d']);
      return data;
    }
    return RightCheck();
  }

  ///比赛详情ai
  static Future<List<ZqModelEntity>> getMatchModel(int id) async {
    final result = await DioUtils.post("/${ApiDir.ai}/app/matchAi", params: id);
    if (result.statusCode == 200 && result.data['c'] == 200) {
      List<ZqModelEntity> data = result.data['d']
          .map<ZqModelEntity>((e) => ZqModelEntity.fromJson(e))
          .toList();
      return data;
    }
    return [];
  }

  ///ai统计
  static Future<List<AiInfoEntity>> getAiInfo(int id) async {
    final result = await DioUtils.post("/${ApiDir.ai}/app-stat/aiStat",
        params: {"type": id});
    if (result.statusCode == 200 && result.data['c'] == 200) {
      List<AiInfoEntity> data = result.data['d']
          .map<AiInfoEntity>((e) => AiInfoEntity.fromJson(e))
          .toList();
      return data;
    }
    return [];
  }

  ///ai统计联赛胜率
  static Future<List<AiRateEntity>> getAiRate(int id) async {
    final result = await DioUtils.post("/${ApiDir.ai}/app-stat/league",
        params: {"type": id});
    if (result.statusCode == 200 && result.data['c'] == 200) {
      List<AiRateEntity> data = result.data['d']
          .map<AiRateEntity>((e) => AiRateEntity.fromJson(e))
          .toList();
      return data;
    }
    return [];
  }

  ///ai模型组合
  static Future<AiPackEntity> getAiPack() async {
    final result = await DioUtils.post("/${ApiDir.ai}/app/aiGroup");
    try {
      if (result.statusCode == 200 && result.data['c'] == 200) {
        AiPackEntity data = AiPackEntity.fromJson(result.data['d']);
        return data;
      }
    } catch (e) {
      log("$e");
      return AiPackEntity();
    }
    return AiPackEntity();
  }
}
